<?php error_reporting (E_ALL ^ E_NOTICE); // Remove unneeded notices?> 
<?php
	
    // cas-demo.php - Demo of PHP using the Web Login Service
    //              - 06/08/07, Russell Tokuyama
    //              - Copyright (c) University of Hawaii 2007
    //                See the end of this file for the copyright notice.
    //
    //-------------------------------------------------------------
    //
    // The only things that need to be configured:
    //
    //-------------------------------------------------------------
    // 1. Set this to the URL of this script.
    $me = "http://localhost/index.php";
    //$me = "welcome.php";
    // Game Link
    $game = "http://localhost/A5/statesGame.php";
    //$game = "A5/statesGame.php";
    // 2. Set this to the base URL of the Web Login Service (no trailing 
    //    slash).
    $weblogin = "https://login.its.hawaii.edu/cas";

    //-------------------------------------------------------------
    //
    // These are based on the above variables.
    //
    // Login link to the Web Login Service
    $login = "${weblogin}/login?service=" . urlencode($me);

    // Logout link to the Web Login Service
    $logout = "${weblogin}/logout?service=" . urlencode($me) .
              urlencode("?logoff=yes");
    //-------------------------------------------------------------
    //
    // For help with problems, look in your Apache web server's error_log
    // file.
    //
    //-------------------------------------------------------------

    //-------------------------------------------------------------
    // This function will call the Web Login Service's validate URL on
    // the service ticket returned when a user logins there.  It returns
    // the elements of the validation response.  See the Web Login
    // Service documentation at
    // http://www.hawaii.edu/infotech/middleware/weblogin/.
    //-------------------------------------------------------------

    function validate($svc, $tkt) {
        global $weblogin;

        // validation URL of the Web Login Service
        $validate_url = "$weblogin/validate" .
                        '?service=' . urlencode($svc) .
                        '&ticket=' . urlencode($tkt);

        // Call the validation service
        error_log('validate(): validating [' . $validate_url . ']');
        $resp = file_get_contents($validate_url );

        if (empty($resp)) {
            error_log('validate(): empty response');
        }
        else {
            // Parse the validation response to get the username and
            // person info for the authenticated user.
            list($status, $uid, $uhuuid, $name, $affil) = explode("\n", $resp);

            if (empty($status)) {
                error_log('validate(): response didn\'t explode');
            }

            return array($status, $uid, $uhuuid, $name, $affil);
        }
        return;
    }

    //-------------------------------------------------------------
    // This part of the script handles calling the Web Login Service
    // to authenticate the user if the user doesn't have a session yet,
    // validating the service ticket after they are authenticated, and
    // handling the user's session which is independent of the
    // authentication with the Web Login Service.
    //-------------------------------------------------------------

    session_start();

    if ($_GET['logoff']) {
        session_destroy();
        header("Location: $me");
        exit;
    }

    if (!isset($_SESSION['name'])) { // not logged in
        $ticket = $_GET['ticket'];

        if (! empty($ticket)) {
            // validate service ticket
            list ($status, $uid, $uhuuid, $name, $affs) =
                validate($me, $ticket);

            if ($status == 'yes') {
                // save data in session
                $_SESSION['name']   = "$name";
                $_SESSION['uid']    = "$uid";
                $_SESSION['affs']   = "$affs";
                $_SESSION['count']  = 1;
                $name = "$name";

                // Get rid of the query parameters in the URL.
                header("Location: $me");  // redirect to me
                exit;
            } else {
                if (empty($status)) {
                    error_log('status is empty');
                    $action = 'network_error';
                }
                else {
                    error_log('status: ' . $status);

                    // tried to reuse the service ticket
                    $action = 'reuse_error';
                    session_destroy();
                }
            }
        }
        else {
            error_log('ticket is empty');
            $action = 'empty_ticket';
        }
    } else {  // previously logged in
        $action = 'continue';

        // get data from session
        $name   = $_SESSION['name'];
        $uid    = $_SESSION['uid'];
        $affs   = $_SESSION['affs'];
        $count  = $_SESSION['count'];

        // bump our counter
        $_SESSION['count']++;
    }              
?>
<?php
    //-------------------------------------------------------------
    // Copyright (c) 2011 University of Hawaii.  All rights reserved.
    //
    // Redistribution and use in source and binary forms, with or without
    // modification, are permitted provided that the following conditions
    // are met:
    //
    // 1. Redistributions of source code must retain the above copyright
    //    notice, this list of conditions and the following disclaimer.
    //
    // 2. Redistributions in binary form must reproduce the above copyright
    //    notice, this list of conditions and the following disclaimer in
    //    the documentation and/or other materials provided with the
    //    distribution.
    //
    // 3. Redistributions of any form whatsoever must retain the following
    //    acknowledgment:
    //    "This product includes software developed by the University of
    //    Hawaii (http://www.hawaii.edu/)."
    //
    // THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF HAWAII "AS IS" AND ANY
    // EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OF HAWAII
    // OR THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    // OF THE POSSIBILITY OF SUCH DAMAGE.
    //-------------------------------------------------------------
?>